//
//  CDTestViewController4.m
//  CuriosityDaily3
//
//  Created by JSY on 16/9/23.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDTestViewController4.h"
#import "CDCuriosityViewCell.h"
#import "CDPullToLoadView.h"

static NSString *cellid = @"cellid";

@interface CDTestViewController4 ()<UITableViewDelegate,UITableViewDataSource>


@property(nonatomic,weak)UITableView *tableview;

@property(nonatomic,strong)NSMutableArray *array;

@property(nonatomic,assign)CGFloat maxOfsetY;

@end

@implementation CDTestViewController4

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor randomColor];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.titleTextAttributes = @{ NSForegroundColorAttributeName: [UIColor colorWithHex:0x3e3e3e] };
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
    [self loaddata];
    [self setupUI];
    [self.tableview reloadData];
    
}


-(void)setupUI{
    
    self.maxOfsetY = 0;
    UITableView *tableview = [[UITableView alloc]init];
    [self.view addSubview:tableview];
    [tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.tableview = tableview;
    tableview.delegate = self;
    tableview.dataSource = self;
    tableview.rowHeight = UITableViewAutomaticDimension;
    tableview.estimatedRowHeight = 100;
    [tableview registerClass:[CDCuriosityViewCell class] forCellReuseIdentifier:cellid];
    
    CDPullToLoadView *pullview = [[CDPullToLoadView alloc]init];
    pullview.frame = CGRectMake(0, 0, 0, 100);
    tableview.tableFooterView = pullview;
    
    
}



-(void)loaddata{
    NSString *urlstr = @"http://app3.qdaily.com/app3/papers/index/0.json?";
    
    AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
    [manger GET:urlstr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSDictionary *dic2 = dic[@"response"];
        NSArray *feeds = dic2[@"feeds"];
        NSMutableArray *muarr = [NSMutableArray array];
        for (NSDictionary *dic in feeds) {
            NSDictionary *postDic = dic[@"post"];
            CDCuriosityModel *model = [[CDCuriosityModel alloc]init];
            [model setValuesForKeysWithDictionary:postDic];
            [muarr addObject:model];
        }
        self.array = muarr.copy;
        
        [self.tableview reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"数据请求超时"];
        
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CDCuriosityViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid forIndexPath:indexPath];
    cell.model = self.array[indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    NSIndexPath *path = [tableView indexPathForCell:cell];
    if (self.array.count == path.row + 4) {
        CDCuriosityModel *model = self.array.lastObject;
        NSString *lastKey = [NSString stringWithFormat:@"%zd",model.publish_time];
        NSString *urlstr = [NSString stringWithFormat:@"http://app3.qdaily.com/app3/papers/index/%@.json?",lastKey];
        
        AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
        [manger GET:urlstr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSDictionary *dic = responseObject;
            NSDictionary *dic2 = dic[@"response"];
            NSArray *feeds = dic2[@"feeds"];
            NSMutableArray *muarr = [NSMutableArray array];
            for (NSDictionary *dic in feeds) {
                NSDictionary *postDic = dic[@"post"];
                CDCuriosityModel *model = [[CDCuriosityModel alloc]init];
                [model setValuesForKeysWithDictionary:postDic];
                [muarr addObject:model];
            }
            NSMutableArray *temparr = [NSMutableArray arrayWithArray:self.array];
            [temparr addObjectsFromArray:muarr];
            self.array = temparr;
            
            [self.tableview reloadData];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [SVProgressHUD showErrorWithStatus:@"数据请求超时"];
            
        }];
    }
    
}



-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//        NSLog(@"%f",scrollView.contentOffset.y);
    if (self.maxOfsetY > scrollView.contentOffset.y) {
        self.maxOfsetY = scrollView.contentOffset.y;
        NSLog(@"%f",self.maxOfsetY);
    }
//        scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
//        NSString *urlstr = @"http://app3.qdaily.com/app3/papers/index/0.json?";
//        
//        AFHTTPSessionManager *manger = [AFHTTPSessionManager manager];
//        [manger GET:urlstr parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//            NSDictionary *dic = responseObject;
//            NSDictionary *dic2 = dic[@"response"];
//            NSArray *feeds = dic2[@"feeds"];
//            NSMutableArray *muarr = [NSMutableArray array];
//            for (NSDictionary *dic in feeds) {
//                NSDictionary *postDic = dic[@"post"];
//                CDCuriosityModel *model = [[CDCuriosityModel alloc]init];
//                [model setValuesForKeysWithDictionary:postDic];
//                [muarr addObject:model];
//            }
//            [muarr addObjectsFromArray:self.array];
//            self.array = muarr.copy;
//            
//            [self.tableview reloadData];
//            isRefresh = YES;
//            NSLog(@"刷新数据");
//        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//            [SVProgressHUD showErrorWithStatus:@"数据请求超时"];
//            
//        }];
    
    
}

-(void)pulldown{



}
//http://app3.qdaily.com/app3/papers/detail/1241.json? //对应详情数据 拼接id
//http://app3.qdaily.com/app3/options/index/1241/1.json? //上拉刷新 拼接刷新次数

@end
